package ru.sgu.codecenter.fidx;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Configuration {
    private static final Properties properties = new Properties();

    public static File getStorageDir() {
        String path = properties.getProperty("fidx.storage-dir");
        File storageDir = new File(path);
        if (!storageDir.exists()) {
            if (!storageDir.mkdir()) {
                throw new OperationFailedException("Unable to create storage directory [path=\"" + path + "\"]");
            }
        }

        return storageDir;
    }

    public static int getHitsCount() {
        return Integer.parseInt(properties.getProperty("fidx.hits-count"));
    }

    public static int getMaxFileSize() {
        return Integer.parseInt(properties.getProperty("fidx.max-file-size"));
    }

    public static int getInserterThreadCount() {
        return Integer.parseInt(properties.getProperty("fidx.get-inserter-thread-count"));
    }

    public static String[] getSkippingExtensions() {
        return properties.getProperty("fidx.skipping-extensions").split(";");
    }

    static {
        InputStream propertiesInputStream = Configuration.class.getResourceAsStream("/fidx.properties");
        try {
            properties.load(propertiesInputStream);
        } catch (IOException e) {
            throw new RuntimeException("Unable to read /fidx.properties.", e);
        } finally {
            if (propertiesInputStream != null) {
                try {
                    propertiesInputStream.close();
                } catch (IOException ignored) {
                    // No operations.
                }
            }
        }
    }
}
